package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Meal;
import com.wn.class30.service.BackMealService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BackMealController {
    @Resource
    private BackMealService backMealService;
    @RequestMapping("/houtai/page/user/meal/list")
    @RequiresPermissions("/houtai/page/user/meal/list")
    public Result getList()throws Exception{
        return new Result("200","success",null,null);
    }
    @RequestMapping("/houtai/user/getMeals")
    public Result getFranchiseesByPage(Integer page,Integer limit) throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf(page));
        map.put("limit",String.valueOf(limit) );

        Page pager = new Page();
        pager.setCurrentPage(page);
        pager.setPageSize(limit);
        int count = backMealService.selectCount((HashMap) map);
        pager.setTotalSize(count);
        pager.setTotalPage(count%limit==0?count/limit:count/limit+1);
        return new Result("0","success",pager, (List) backMealService.select((HashMap) map));
    }
    @RequestMapping("/houtai/page/user/meal/delete")
    @RequiresPermissions("/houtai/page/user/meal/delete")
    public Result deleteMeals(@RequestParam("mealIds[]") Integer[] mealIds)throws Exception{

        for (Integer mealId : mealIds) {
            backMealService.deleteById(mealId);
        }

        return new Result("0","success",null, null);
    }
    @RequestMapping("/houtai/page/user/meal/update")
    @RequiresPermissions("/houtai/page/user/meal/update")
    public Result updateMeal(Meal meal)throws Exception{
        backMealService.updateMeal(meal);
        return new Result("0","success",null, null);
    }
    @RequestMapping("/houtai/page/user/meal/insert")
    @RequiresPermissions("/houtai/page/user/meal/insert")
    public Result insertMeal(Meal meal)throws Exception{
        //meal.setMealCreatetime(new Date());
        backMealService.insertMeal(meal);
        return new Result("0","success",null, null);
    }
}
